import {createSlice,PayloadAction,createAsyncThunk} from '@reduxjs/toolkit'
import axios from 'axios'
interface DetailitemState{
    isloading:boolean,
    error:null|string,
    detailitem:any,
}
const initialState:DetailitemState={
    isloading:true,
    error:null,
    detailitem:null,
}
export const getProductDetail=createAsyncThunk(
    "detailItem/getProductDetail",
    // async (touristid:string,thunkAPI)=>{
    //     try{
    //         const {data} = await axios.get(`http://123.56.149.216:8080/api/touristRoutes/${touristid}`, {
    //             headers: {"x-icode": "69ACF3816BF5D32D"}
    //         })
    //         thunkAPI.dispatch(detailitemSlice.actions.requestSuccess(data));
    //     }catch (e){
    //         thunkAPI.dispatch(detailitemSlice.actions.requestFail(e.message));
    //     }
    // }
    async (touristid:string)=>{
        const {data} = await axios.get(`http://123.56.149.216:8080/api/touristRoutes/${touristid}`, {
            headers: {"x-icode": "69ACF3816BF5D32D"}
        })
        return data;//这样就可以用到pending...三个状态
    }
)
export const detailitemSlice=createSlice({
    name:"detailItem",
    initialState,
    // reducers:{
    //     requestLoading:(state)=>{
    //         state.isloading=true;
    //     },
    //     requestSuccess:(state,action:PayloadAction<any[]>)=>{
    //         state.isloading=false;
    //         state.detailitem=action.payload;
    //     },
    //     requestFail:(state,action:PayloadAction<string|null>)=>{
    //         state.isloading=false;
    //         state.error=action.payload;
    //     }
    // }
    reducers:{},
    extraReducers:{
        [getProductDetail.pending.type]:(state)=>{
            state.isloading=true;
        },
        [getProductDetail.fulfilled.type]:(state,action:PayloadAction<any[]>)=>{
            state.isloading=false;
            state.detailitem=action.payload;
        },
        [getProductDetail.rejected.type]:(state,action)=>{
            state.isloading=false;
            state.error=action.error.message;
        }
    }
})